package cn.edu.stu.syllabus.utils;

import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;

import java.io.File;

import cn.edu.stu.syllabus.application.SyllabusApplication;

/**
 * Created by daidaijie on 2017/4/9.
 */

public class FunctionCaller {

    public static boolean isVaildIntent(Context context, Intent intent) {
        return intent.resolveActivity(context.getPackageManager()) != null;
    }


    public static void callPhone(Context context, String phoneNumber) {
        Intent intent = new Intent(Intent.ACTION_DIAL);
        intent.setData(Uri.parse("tel:" + phoneNumber));
        if (isVaildIntent(context, intent)) {
            context.startActivity(intent);
        }
    }

    public static void openGallery(Context context, File photoFile) {
        Intent intent = new Intent("android.intent.action.VIEW");
        intent.addCategory("android.intent.category.DEFAULT");
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

        Uri photoURI = Android_N_Utils.getFileUri(photoFile);
        intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
        intent.setDataAndType(photoURI, "image/*");
        if (isVaildIntent(context, intent)) {
            context.startActivity(intent);
        }
    }

    public static void copy2Clipboard(String text) {
        ClipboardManager clipboard = (ClipboardManager)
                SyllabusApplication.getContext().getSystemService(Context.CLIPBOARD_SERVICE);
        ClipData clipData;
        clipData = ClipData.newPlainText("text"
                , text);
        clipboard.setPrimaryClip(clipData);
    }
}
